package com.leetcode.problems020;


import com.leetcode.util.ListNode;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/valid-parentheses/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public boolean isValid(String s) {
        if(s == null || s.length() == 0){
            return true;
        }
        int n = s.length();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < n; i++) {
            if(isRightChar(s.charAt(i))){
                if(stack.isEmpty()){
                    return false;
                }
                if(s.charAt(i) == '}' && stack.pop().charValue() != '{'){
                    return false;
                }
                if(s.charAt(i) == ')' && stack.pop().charValue() != '('){
                    return false;
                }
                if(s.charAt(i) == ']' && stack.pop().charValue() != '['){
                    return false;
                }
            }else{
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();
    }

    public boolean isRightChar(char c){
        if(c == '}' || c == ')' || c == ']'){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        String str = "[]({})";
        System.out.println(new Solution().isValid(str));
    }
}
